#include <pd_timer.h>
#include <hd_led.h>
#include <hd_oled.h>
#include <delay.h>
#include <hd_oled_protocol_spi.h>
#include <hd_oled_protocol_i2c.h>
#include <pd_timer_oc.h>
#include <pd_timer_pwm.h>
#include <pd_i2c_hd.h>
#include <hd_flash.h>
#include "main.h"

#define OLED_PROTOCOL       2   // Select a protocol. SPI, 1; I2C, 2;
#define OLED_PROTOCOL_IMPL  2   // Select a implement. simulate, 1; hardware, 2;

LED *led;
static OLED *oled;

void oled_instance();
extern void SystemClock_Config(void);


int main(void) {
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

    SystemClock_Config();

    NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

    GPIO *ledGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_2);
    led = LEDBuilder.build(ledGPIO);
    led->init(led);

    oled_instance();

    oled->showString(oled, 0, 0, "TIM PWM LED!");
    oled->showString(oled, 0, 16, "GPIOB 6/7/8/9");
    oled->refresh(oled);

    // PB6 -- TIM4_CH1; PB7 -- TIM4_CH2; PB8 -- TIM4_CH3; PB9 -- TIM4_CH4
#ifdef MORE_CHANNEL
    GPIO *gpios[4];

    GPIO *gpio0 = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_6);
    gpio0->Mode = LL_GPIO_MODE_ALTERNATE;
    gpio0->Alternate = LL_GPIO_AF_2;
    gpios[0] = gpio0;

    GPIO *gpio1 = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_7);
    gpio1->Mode = LL_GPIO_MODE_ALTERNATE;
    gpio1->Alternate = LL_GPIO_AF_2;
    gpios[1] = gpio1;

    GPIO *gpio2 = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_8);
    gpio2->Mode = LL_GPIO_MODE_ALTERNATE;
    gpio2->Alternate = LL_GPIO_AF_2;
    gpios[2] = gpio2;

    GPIO *gpio3 = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_9);
    gpio3->Mode = LL_GPIO_MODE_ALTERNATE;
    gpio3->Alternate = LL_GPIO_AF_2;
    gpios[3] = gpio3;
#else
    GPIO *chGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_6 | LL_GPIO_PIN_7 | LL_GPIO_PIN_8 | LL_GPIO_PIN_9);
    chGPIO->Mode = LL_GPIO_MODE_ALTERNATE;
    chGPIO->Alternate = LL_GPIO_AF_2;
    GPIO *gpios[1] = {chGPIO};
#endif

    uint32_t channel[] = {LL_TIM_CHANNEL_CH1, LL_TIM_CHANNEL_CH2, LL_TIM_CHANNEL_CH3, LL_TIM_CHANNEL_CH4};
    TimerCh *timerCh = TimerChBuilder.build(gpios, 1, channel, 4);

    // freq = 1kHz, duty is variable, resolution = 1%
    Timer *timer = TimerBuilder.build(TIM4, 840 - 1, 100 - 1);
    TimerOC *timerOC = TimerOCBuilder.build(timer, timerCh, 0);
    TimerPWM *timerPWM = TimerPWMBuilder.build(timer, timerOC);
    timerPWM->init(timerPWM);

    while (1) {
        for (int i = 0; i < 100; ++i) {
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH1, i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH2, 100 - i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH3, i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH4, 100 - i);
            delay_ms(10);
        }

        delay_ms(100);

        for (int i = 0; i < 100; ++i) {
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH1, 100 - i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH2, i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH3, 100 - i);
            timerPWM->setPWMDuty(timerPWM, LL_TIM_CHANNEL_CH4, i);
            delay_ms(10);
        }

        delay_ms(100);
    }
}

void oled_instance() {
#if OLED_PROTOCOL == 1
    GPIO *sckGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_5); // Clock, SPIx_SCK
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_7); // MOSI,  SPIx_MOSI
    GPIO *resGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_3); // Reset, General IO, the chip reset pin is better
    GPIO *dcGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_1);  // Data or Command, General IO
    GPIO *csGPIO = GPIOBuilder.build(GPIOA, LL_GPIO_PIN_2);  // Chip Select(Slave Select), General IO
    SPIGPIO *spiGPIO = SPIGPIOBuilder.build(&csGPIO, 1, sckGPIO, sdaGPIO, NULL);

#if OLED_PROTOCOL_IMPL == 1
    SPISimulate *spiObj = SPISimulateBuilder.build(spiGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    SPIHardware *spiObj = SPIHardwareBuilder.build(SPI1, spiGPIO);
#endif
    SPI *spi1 = (SPI *) spiObj;
    spi1->init(spi1);
    OLEDProtocolSPI *oledProtocolObj = OLEDProtocolSPIBuilder.build(spi1, resGPIO, dcGPIO, csGPIO);

#elif OLED_PROTOCOL == 2
    GPIO *sckGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_10);  // I2Cx_SCL
    GPIO *sdaGPIO = GPIOBuilder.build(GPIOB, LL_GPIO_PIN_11);  // I2Cx_SDA
#if OLED_PROTOCOL_IMPL == 1
    I2CSimulate *i2CObj = I2CSimulateBuilder.build(sckGPIO, sdaGPIO);
#elif OLED_PROTOCOL_IMPL == 2
    I2CHardware *i2CObj = I2CHardwareBuilder.build(I2C2, sckGPIO, sdaGPIO);
#endif
    I2C *i2c = (I2C *) i2CObj;
    i2c->init(i2c);
    OLEDProtocolI2C *oledProtocolObj = OLEDProtocolI2CBuilder.build(i2c);
#endif

    StorageFlash *storageFlash = StorageFlashBuilder.build();
    OLEDFont *oledFont = OLEDFontBuilder.build((Storage *) storageFlash, ENCODING_UTF8);
    oled = OLEDBuilder.build((OLEDProtocol *) oledProtocolObj, oledFont);
    oled->init(oled);
}


